#!/bin/bash

path=$2
echo "target-dir=$path"

exports(){

    export CANGJIE_STDX_PATH=$CANGJIE_STDX_DYNAMIC_PATH

    # pattern可省略，有默认值
    export logger_appender_console=FDemoConsole
    # 如果使用 DEBUG，会有很多信息输出（比如定时任务 每秒都会输出）。视情况修改为 DEBUG
    export logger_appender_FDemoConsole_level=DEBUG

    export logger_appender_FDemoConsole_pattern='[%level-%name]%d{yyyy/MM/dd,HH:mm:ss.SSS}|%m'
    export logger_appender_file=FDemoFile
    export logger_appender_FDemoFile_level=INFO
    export logger_appender_FDemoFile_pattern='[%level-%name]%d{yyyy/MM/dd,HH:mm:ss.SSS}|%m'
    export logger_appender_FDemoFile_path=./log/fdemo.log
    export logger_appender_FDemoFile_rotateDuration=DAY

    export mvc_port=8080 # 这一行可以没有，默认就是8080
    export mvc_overallElapsedSwitch=true
    export mvc_internalServerErrorMessageKind=BEAN
    export mvc_internalServerErrorMessage=NameOf500Handler



    export orm_noPool=true # 默认是false，true表示不用连接池

    export orm_useStdPool=false # 默认是true
    export orm_drivers=opengauss
    export orm_databasePoolInitSize=10
    export orm_databasePoolMinSize=10
    export orm_databasePoolMaxSize=10
    export orm_databasePoolCheckOnCreation=true
    export orm_databasePoolCheckOnBorrowing=true
    export orm_databasePoolCheckOnReturning=false
    export orm_databasePoolConnectionLife=86400
    export orm_databasePoolCheckInterval=300 # 默认是300，单位是秒
    export orm_databasePoolCheckSql='select 1'

    #    数据库连接是这里
    export opengauss_orm_connectionUrl=$DB_URL

# 这几行 删除或注释掉
#    export orm_pooledDatasourceMaxSize=1
#    export orm_pooledDatasourceMaxIdleSize=1
#    export orm_pooledDatasourceIdleTimeout=86400
#    export orm_stdPoolMaxLifeTime=86400
#    export orm_pooledDatasourceConnectionTimeout=86400
#    export orm_pooledDatasourceKeepaliveTime=86400


    # orm_transactionalFuncExecution 和@Transactional注解只要有一个生效就会将事务切面织入到函数
#    export orm_transactionalFuncExecution='*..*.delete*(**): *|*..*.remove*(**): *|*..*.save*(**): *|*..*.add*(**): *|*..*.new*(**): *|*..*.create*(**): *|*..*.insert*(**): *|*..*.update*(**): *|*..*.change*(**): *|*..*.register*(**): *'
#    export orm_transactionalFuncExecution='*..*.change*(**): *'

    if [[ "$path" == "" ]]; then
        path='./fdemo'
    fi

    export LD_LIBRARY_PATH=$path/release/boot:$path/release/opengauss:$path/release/examples:$LD_LIBRARY_PATH

    export PATH=/root/.cjpm/bin:/root/.cjpm/libs/fboot:$PATH
}

run(){
    importEnv
    exports

#    fboot run $path --dylibPattern='(boot|user\.util\.auth|\.(controller|service\.impl))'
    fboot run $path --dylibPattern='(boot|examples\.exceptions|\.(controller|service\.impl))'
}

build(){
    export CANGJIE_STDX=$CANGJIE_STDX_DYNAMIC_PATH
    export CANGJIE_STDX_PATH=$CANGJIE_STDX_DYNAMIC_PATH

    importEnv

    # 存在 cjpm.tpl.toml 时，将cjpm.tpl.toml 中的变量替换后生成 cjpm.toml
    # 替换指定变量。( $TEST_VERSION 用来演示写法的，无实际意义)
    [ -f cjpm.tpl.toml ]  && {
      envsubst '$FOUNTAIN_COMMIT_ID,$TEST_VERSION' < cjpm.tpl.toml > cjpm.toml
    }

    fboot build $path
    echo -e '\a'
}

cleanUpdate(){
    fboot cleanUpdate $path
    echo -e '\a'
}

importEnv(){
# 作用是把 .env 文件中的变量 导入到 环境变量，等同于 export
# 如果你使用脚本/构建平台变量 管理环境变量，可忽略此方法

    if [ -f .env ]; then
        echo "找到 .env 文件，导入环境变量..."
        set -a
        # grep -v '^[[:space:]]*#' 是为了忽略 .env 文件中#开头的注释
        source <(grep -v '^[[:space:]]*#' .env)
        set +a
        echo "环境变量导入成功"

    else
        echo "错误: 未找到 .env 文件。 "
        echo "请生成此文件： cp env.example .env"
        exit 1
    fi
}



case "$1" in
run)
    run
    ;;
cleanUpdate)
    cleanUpdate $2 $3
    ;;
build)
    build
    ;;
launch)
    launch
    ;;
esac

exit $?
